package br.com.news.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import br.com.news.R;

public class DataUtil {

	
	public static final long SEGUNDO = 1000L;
	public static final long MINUTO = 60000L;
	public static final long HORA = 3600000L;
	public static final long DIA = 86400000L;
	public static final long SEMANA = 604800000L;
	
	
	public static Date parse(String data) throws ParseException{
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		return dateFormat.parse(data);
	}
	
	public static String format(Date data){
		if ( data == null) return null;
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		return dateFormat.format(data);
	}
	
	public static long diferencaDatas(Date inic, Date fim){
		if (fim == null) {
			fim = new Date();
		}
		if (inic == null) {
			return 0;
		}
		return fim.getTime() - inic.getTime();
	}
	
	
	public static String mostrarDif(long dif, Context context){
		
		if(Math.abs(dif) < HORA){
			return difMinuto(dif, context);
		}else if(Math.abs(dif) < DIA){
			return difHora(dif, context);
		}else if(Math.abs(dif) < SEMANA){
			return difDia(dif, context);
		}else{
			return difSemanda(Math.abs(dif), context);
		}
	}
	
	private static String difMinuto(long dif, Context context){
		return String.format("%d %s",Math.abs(dif/MINUTO), context.getString(R.string.txt_minutos));
	}
	
	private static String difHora(long dif, Context context){
		return String.format("%d %s",Math.abs(dif/HORA), context.getString(R.string.txt_horas));
	}
	
	private static String difDia(long dif, Context context){
		return String.format("%d %s",Math.abs(dif/DIA), context.getString(R.string.txt_dias));
	}
	
	private static String difSemanda(long dif, Context context){
		return String.format("%d %s",Math.abs(dif/SEMANA), context.getString(R.string.txt_semanas));
	}
}
